﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Common;

public class ScoreItem
{
    public ScoreItem(Transform root, Transform transform, int score)
    {
        posTransform = transform;
        position = posTransform.position;
        Root = root;
        if (score >= 0)
            showStr = "+" + score.ToString();
        else
            showStr = score.ToString();
        tmp = Root.Find("p").gameObject;
        tmp.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - ((showStr.Length * ScoreManager.TextureSize) / 2.0f) + (ScoreManager.TextureSize / 2.0f));
        SetNumber(tmp, showStr[0]);
        sList.Add(tmp);
        for (int i = 1; i < showStr.Length; i++)
        {
            GameObject g = GameObject.Instantiate(tmp) as GameObject;
            g.transform.position = sList[sList.Count - 1].transform.position + Vector3.forward * ScoreManager.TextureSize;
            g.transform.parent = Root;
            SetNumber(g, showStr[i]);
            sList.Add(g);
        }
    }
    public bool End = false;
    public Transform Root;
    private Vector3 position;
    private Transform posTransform;
    private float height = 0;
    public void Update()
    {
        if (posTransform != null) position = posTransform.position;
        if (transBig)
        {
            if (Root.localScale.y > ScoreManager.MaxScale.y && Root.localScale.z > ScoreManager.MaxScale.z)
            {
                transBig = false;
            }
            else
            {
                Root.localScale += Vector3.one * ScoreManager.ScaleSpeed * InputManager.DeltaTime;
            }
        }
        else
        {
            if (Root.localScale.y < ScoreManager.MinScale.y && Root.localScale.z < ScoreManager.MinScale.z)
            {
                End = true;
                Root.gameObject.SetActiveRecursively(false);
            }
            else
            {
                Root.localScale -= Vector3.one * ScoreManager.ScaleSpeed * InputManager.DeltaTime;
            }
        }
        height += ScoreManager.UpSpeed * InputManager.DeltaTime;
        Root.position = position + Vector3.up * height;
    }

    private GameObject tmp = null;
    private List<GameObject> sList = new List<GameObject>();
    private string showStr = string.Empty;
    private bool transBig = true;
    void SetNumber(GameObject g, char number)
    {
        g.renderer.material.mainTextureScale = new Vector2(1 / ScoreManager.FontCount, 1);
        switch (number)
        {
            case '+':
                {
                    g.renderer.material.mainTextureOffset = new Vector2(10 / ScoreManager.FontCount, 1);
                    break;
                }
            case '-':
                {
                    g.renderer.material.mainTextureOffset = new Vector2(11 / ScoreManager.FontCount, 1);
                    break;
                }
            default:
                {
                    g.renderer.material.mainTextureOffset = new Vector2(float.Parse(number.ToString()) / ScoreManager.FontCount, 1);
                    break;
                }
        }
    }
}
